<template>
	<router-view v-if="isRouterAlive" />
</template>

<script>
    import {onUnmounted, provide} from "vue";
    import {useStore} from "vuex";
    import * as echarts from 'echarts'
    import {refreshFromHis} from "./api/refresh";

    export default {
        name: 'App',
        provide() {
            return {
                reload: this.reload,
                'ec': echarts
            }
        },
        setup() {
            const store = useStore();
            let intervalId=null;
            const dataRefresh=()=> {
                // 计时器正在进行中，退出函数
                if (intervalId != null) {
                    return;
                }
                // 计时器为空，操作
                intervalId = setInterval(() => {
                  if(localStorage.getItem('deviceId') !== null) {
                      // console.log("========定时刷新");
                      refreshFromHis().then((res)=>{
                          // console.log("========收到的结果为",res)
                          if(res.data==null){
                              // console.log("========收到 null，请求数据")
                              store.commit('refresh');
                          }
                      })

                      // store.commit('refresh')
                  }
                }, 60000);
            }
            // 停止定时器
            const clear=()=> {
                clearInterval(intervalId); //清除计时器
                intervalId = null; //设置为null
            }
            if(localStorage.getItem('deviceId') !== null){
               store.commit('refresh')
            }
            dataRefresh();
            onUnmounted(()=>{
                clear();
            })
            return {
                isRouterAlive: true
            }
        },
        methods: {
            //先关闭再打开
            reload() {
                this.isRouterAlive = false;
                this.$nextTick(() => {
                    this.isRouterAlive = true;
                })
            }
        }
    };
</script>

<style>
    @import "./assets/css/main.css";
    @import "./assets/css/color-dark.css";
</style>